define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/agentorder/index',
                    table: 'y_onlineorder',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'ID',
                sortName: 'ID',
                search: false,
                columns: [
                    [
                        {field: 'ID', title: __('Id'), operate: false},
                        {field: 'orderID', title: __('Orderid'), operate: false},
                        {field: 'uid', title: __('Uid')},
                        {field: 'nickname', title: __('Nickname'), operate: false},
                        {field: 'bfnum', title: __('Bfnum'), operate: false},
                        {field: 'afnum', title: __('Afnum'), operate: false},
                        {field: 'orderamount', title: __('Orderamount'), operate: false},
                        {field: 'quotanum', title: __('Quotanum'), operate: false},
                        {field: 'givenum', title: __('Givenum'), operate: false},
                        {field: 'addtime', title: __('Addtime'), formatter: Table.api.formatter.datetimes, operate: 'BETWEEN', type: 'datetime', addclass: 'datetimepicker', data: 'data-date-format="YYYY-MM-DD"', style: 'min-width:90px;'},
                        {field: 'status', title: __('Status'), searchList: {'0':'未支付','1':'支付失败', '2': '支付成功'}},
                        {field: 'payment', title: __('Payment'), searchList: {'0':'支付宝', '1': '微信', '3': '苹果'}, operate: false},
                    ]
                ],
                
	            onLoadSuccess: function (data) {
					if(data.map == 1){
						$("#statistics").html(
								'<a class="btn btn-danger">总充值：' + data.statistics.totalall + '</a>&nbsp;' 
								);
		            	$("#statistics").show();
		            }else{
						$("#statistics").html(
								'<a class="btn btn-danger">总充值：' + data.statistics.totalall + '</a>&nbsp;' 
								+ '<a class="btn btn-success">月充值：' + data.statistics.totalmonth + '</a>&nbsp;' 
								+ '<a class="btn btn-info">周充值：' + data.statistics.totalweek + '</a>&nbsp;' 
								+ '<a class="btn btn-warning">今日充值：' + data.statistics.totaltoday + '</a>&nbsp;' 
								);
		            	$("#statistics").show();
		            }
	            }
            
            });
            
            //指定搜索条件
            var sstime = $("#sstime").text();  
            if(sstime != '' && sstime != null){
            	$("#addtime").val(sstime);
            	var a = sstime+','+sstime;
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    return {
                        search: params.search,
                        sort: params.sort,
                        order: params.order,
                        filter: JSON.stringify({addtime: a}),
                        op: JSON.stringify({addtime: 'RANGE'}),
                        offset: params.offset,
                        limit: params.limit,
                    };
                };
                table.bootstrapTable('refresh', {});
            }

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});